package com.foreknow.demo07;

import java.util.Scanner;

public class UserManager {
    /**
     * 1.定义一个方法，用于添加用户
     * 2.根据id查询到某一个用户，查询用户
     * 4.修改用户信息
     * 5.查询所有用户信息
     * */
    Scanner scan = new Scanner(System.in);
    User[] userArray = new User[5];
    /**
     *
     * @return
     */
    public User[] addUser(){
        for (int i = 0;i < userArray.length;i++){
            User user = new User();
            System.out.println("请输入用户姓名");
            String name = scan.next();
            System.out.println("请输入用户性别");
            String sex = scan.next();
            System.out.println("请输入用户年龄");
            int age = scan.nextInt();
            System.out.println("请输入用户爱好");
            String hobby = scan.next();
            System.out.println("请输入用户电话");
            String tel = scan.next();
            System.out.println("请输入用户住址");
            String address = scan.next();
            user.setName(name);
            user.setSex(sex);
            user.setAge(age);
            user.setHobby(hobby);
            user.setTel(tel);
            user.setAddress(address);
            userArray[i] = user;
        }
        return userArray;
    }

    /**
     *
     * @param userArray
     */
    public void selectUser(User[] userArray){
        System.out.println("请输入要查询用户的id");
        int userId = scan.nextInt();
        User userInfo = new User();
        for (int i = 0; i < userArray.length;i++){
            if (userId == i){
                userInfo = userArray[i];
            }
        }
        System.out.println("用户姓名：" + userInfo.getName()+ "用户性别：" + userInfo.getSex()+ "用户年龄：" + userInfo.getAge() + "用户爱好："+userInfo.getHobby()+ "用户电话：" +userInfo.getTel()+ "用户地址：" + userInfo.getAddress());
    }

    /**
     *
     * @param userArray
     */
    public void updateUser(User[] userArray){
        System.out.println("请输入更改用户信息的id");
        int userId = scan.nextInt();
        System.out.println("请输入需要更改的姓名");
        String name = scan.next();
        System.out.println("请输入更改用更改的性别");
        String sex = scan.next();
        System.out.println("请输入更改用户的年龄");
        int age = scan.nextInt();
        System.out.println("请输入更改用户的爱好");
        String hobby = scan.next();
        System.out.println("请输入更改用户的电话");
        String tel = scan.next();
        System.out.println("请输入更改用户的地址");
        String address = scan.next();
        User userInfo = new User();
        for (int i = 0; i < userArray.length;i++){
            if (userId == i){
                userArray[i].setName(name);
                userArray[i].setSex(sex);
                userArray[i].setAge(age);
                userArray[i].setHobby(hobby);
                userArray[i].setTel(tel);
                userArray[i].setAddress(address);
            }
        }
    }

    //查询所有用户信息
    public void selectAllInfo(User[] userAllInfo){
        User userInfo = new User();
        for (int i = 0; i < userAllInfo.length;i++){
            userInfo = userAllInfo[i];
            System.out.println("用户姓名 ：" + userInfo.getName()+ "用户性别：" + userInfo.getSex()+ "用户年龄：" + userInfo.getAge() + "用户爱好："+userInfo.getHobby()+ "用户电话：" +userInfo.getTel()+ "用户地址：" + userInfo.getAddress());
        }
    }
}
